<?php

namespace app\shopping\controller;

use think\Controller;


class Order extends Controller
{
    public function __construct(){
        parent::__construct();
        
        $this->BusinessModel = model('Business.User');
        $this->AddressModel = model('Business.Address');
        $this->OrderModel = model('Product.order.Order');
        $this->OrderProductModel = model('Product.order.Product');
        $this->CartModel = model('Product.Cart');
        $this->ProductModel = model('Product.Product');
        $this->RecordModel = model('Business.Record');
        $this->CommentModel = model('Product.Comment');
        $this->DepotModel = model('Depot.Back');
        $this->DepotProductModel = model('Depot.Productback');
        $this->ExpressModel = model('Express');
    }

    /**
     * 订单列表
     *
     * @ApiTitle    (订单列表)
     * @ApiSummary  (订单列表)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     * @ApiParams   (name="status", type="int", required=true, description="选项卡的选项")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function index(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            $this->business = $this->BusinessModel->find($busid);
    
            if(!$this->business){
                $this->error('用户未知');
                exit;
            }
            $page = $this->request->param('page',1,'trim');
            $status = $this->request->param('status',0,'trim');
            $limit = 8;

            // 偏移量
            $offset = ($page-1) * $limit;

            $where = [
                'busid' => $this->business['id']
            ];

            if($status != 0){
                $where['status'] = $status;
            }

            $list = $this->OrderModel
                ->where($where)
                ->order('createtime','desc')
                ->limit($offset,$limit)
                ->select();
        
            
            if($list){
                foreach($list as $item){
                    $info = $this->OrderProductModel
                        ->with(['products'])
                        ->where(['orderid'=>$item['id']])
                        ->find();
                    
                    $proname = isset($info['products']['name']) ? $info['products']['name'] : '';

                    $thumb_text = isset($info['products']['thumb_text']) ? $info['products']['thumb_text'] : '';

                    $item['proname'] = $proname;
                    $item['thumb_text'] = $thumb_text;
                }
            }

            if($list){
                $this->success('返回订单数据',null,$list);
                exit;
            }else{
                $this->error('暂无更多订单数据返回');
                exit;
            }
        }
    }


    /**
     * 下单
     *
     * @ApiTitle    (下单)
     * @ApiSummary  (下单)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="cartids", type="string", required=true, description="购物车ID")
     * @ApiParams   (name="addrid", type="int", required=true, description="收货地址ID")
     * @ApiParams   (name="remark", type="string", required=false, description="订单备注")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function add(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $cartids = $this->request->param('cartids',0,'trim');
            $addrid = $this->request->param('addrid',0,'trim');
            $remark = $this->request->param('remark','','trim');

            // 先判断用户是否存在
            $business = $this->BusinessModel->find($busid);

            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 判断是否有购物车记录
            $cart = $this->CartModel
                ->with(['product'])
                ->where(['cart.id'=>['in',$cartids]])
                ->select();

            if(!$cart){
                $this->error('购物车记录未知');
                exit;
            }

            $where = [
                'busid' => $busid,
                'id' => $addrid
            ];

            // 判断地址是否存在
            $address = $this->AddressModel->where($where)->find();

            if(!$address){
                $this->error('地址未知');
                exit;
            }

            // 判断库存是否充足
            foreach($cart as $item){
                $stock = isset($item['product']['stock']) ? $item['product']['stock'] : 0;
                $proname = isset($item['product']['name']) ? $item['product']['name'] : '';

                if($item['nums'] > $stock){
                    $this->error("$proname 商品库存不足");
                    exit;
                }
            }

            // 判断余额是否充足
            $total = $this->CartModel->where(['id'=>['in',$cartids]])->sum('total');

            $UpdateMoney = bcsub($business['money'],$total);

            if($UpdateMoney < 0){
                $this->error('余额不足');
                exit;
            }

            // 订单表、订单商品表、商品表、消费记录表、用户表、购物车表

            // 开启事务
            $this->OrderModel->startTrans();
            $this->OrderProductModel->startTrans();
            $this->ProductModel->startTrans();
            $this->BusinessModel->startTrans();
            $this->RecordModel->startTrans();
            $this->CartModel->startTrans();
            
            $OrderData = [
                'code' => build_code('SH'),
                'busid' => $busid,
                'businessaddrid' => $addrid,
                'amount' => $total,
                'remark' => $remark,
                'status' => '1'
            ];

            $OrderStatus = $this->OrderModel->validate('common/Product/order/Order')->save($OrderData);

            if($OrderStatus === FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }

            // 订单商品表
            $OrderData = [];
            $OrderProductData = [];
            $ProductData = [];
            $RecordData = [];

            foreach($cart as $item){
                // 订单表
                $OrderProductData[] = [
                    'orderid' => $this->OrderModel->id,
                    'proid' => $item['proid'],
                    'pronum' => $item['nums'],
                    'price' => $item['price'],
                    'total' => $item['total']
                ];

                $proname = isset($item['product']['name']) ? $item['product']['name'] : '';

                $prounit = $this->ProductModel->with(['unit'])->where(['product.id'=>$item['proid']])->find();

                // 更新商品库存
                $stock = isset($item['product']['stock']) ? $item['product']['stock'] : 0;

                // 更新后的商品库存
                $UpdateStock = bcsub($stock,$item['nums']);
                $UpdateStock = $UpdateStock <= 0 ? 0 : $UpdateStock;

                // 组装数据
                $ProductData[] = [
                    'id' => $item['proid'],
                    'stock' => $UpdateStock
                ];

                // 消费记录
                $RecordData[] = [
                    'total' => -$item['total'],
                    'content' => "购买了 {$item['nums']} {$prounit['unit']['name']} $proname 花费了￥ {$item['total']} 元",
                    'busid' => $item['busid']
                ];
            }

            

            $OrderProductStatus = $this->OrderProductModel->validate('common/Product/order/Product')->saveAll($OrderProductData);

            if($OrderProductStatus === FALSE){
                $this->OrderModel->rollback();
                $this->error($this->OrderProductModel->getError());
                exit;
            }

            // 更新商品库存
            $ProductStatus = $this->ProductModel->isUpdate(true)->saveAll($ProductData);
            
            if($ProductStatus === FALSE){
                $this->OrderProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error($this->ProductModel->getError());
                exit;
            }

            $RecordStatus = $this->RecordModel->validate('common/Business/Record')->saveAll($RecordData);

            if($RecordStatus === FALSE){
                $this->ProductModel->rollback();
                $this->OrderProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error($this->RecordModel->getError());
                exit;
            }

            // 用户余额表更新
            $BusinessData = [
                'id' => $busid,
                'money' => $UpdateMoney,
                'deal' => 1
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->RecordModel->rollback();
                $this->ProductModel->rollback();
                $this->OrderProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error($this->BusinessModel->getError());
                exit;
            }

            // 购物车执行删除
            $CartStatus = $this->CartModel->where(['id' => ['in',$cartids]])->delete();

            if($CartStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->RecordModel->rollback();
                $this->ProductModel->rollback();
                $this->OrderProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error($this->CartModel->getError());
                exit;
            }
            

            if($OrderStatus === FALSE || $OrderProductStatus === FALSE || $ProductStatus === FALSE || $RecordStatus === FALSE || $BusinessStatus === FALSE || $CartStatus === FALSE){
                $this->CartModel->rollback();
                $this->BusinessModel->rollback();
                $this->RecordModel->rollback();
                $this->ProductModel->rollback();
                $this->OrderProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error('下单失败');
                exit;
            }else{
                $this->CartModel->commit();
                $this->BusinessModel->commit();
                $this->RecordModel->commit();
                $this->ProductModel->commit();
                $this->OrderProductModel->commit();
                $this->OrderModel->commit();
                $this->success('下单成功','/order/index');
                exit;
            }
        }
    }


    /**
     * 取消订单
     *
     * @ApiTitle    (取消订单)
     * @ApiSummary  (取消订单)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function cancel(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');

            // 先判断客户是否存在
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 判断订单是否存在
            $order = $this->OrderModel->find($orderid);
            if(!$order){
                $this->error('订单未知');
                exit;
            }

            //订单表 订单商品表 商品表 用户表 消费记录表
            $this->OrderModel->startTrans();
            $this->ProductModel->startTrans();
            $this->BusinessModel->startTrans();
            $this->RecordModel->startTrans();

            // 查询订单商品
            $product = $this->OrderProductModel
                    ->with(['products'])
                    ->where(['orderid'=>$orderid])
                    ->select();
            
            $ProductData = [];
            $RecordData = [];

            foreach($product as $item){
                $pronum = intval($item['pronum']);
                $stock = isset($item['products']['stock']) ? $item['products']['stock'] : 0;
                $stock = intval($stock);

                $ProductData[] = [
                    'id' => $item['proid'],
                    'stock' => bcadd($pronum,$stock)
                ];

                $RecordData[] = [
                    'busid' => $busid,
                    'content' => "退掉商品：{$item['products']['name']} 退款金额：￥ {$item['total']}  元",
                    'total' => $item['total']
                ];

            }
            // var_dump($RecordData);
            // exit;
            // 更新用户余额
            $amount = $order['amount'];
            $money = $business['money'];
            $BusinessData = [
                'id' => $busid,
                'money' => bcadd($amount,$money)
            ];
           
            $OrderData = [
                'id' => $orderid,
                'status' => -1
            ];

            $OrderStatus = $this->OrderModel->isUpdate(true)->save($OrderData);

            if($OrderStatus === FALSE)
            {
                $this->error('退款失败');
                exit;
            }

            // 更新商品库存
            $ProductStatus = $this->ProductModel->isUpdate(true)->saveAll($ProductData);
            
            if($ProductStatus === FALSE){
                $this->OrderModel->rollback();
                $this->error('更新商品库存失败');
                exit;
            }

            // 更新用户余额
            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->ProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error('更新用户余额失败');
                exit;
            }

            // 更新消费记录
            $RecordStatus = $this->RecordModel->validate('common/Business/Record')->saveAll($RecordData);
            
            if($RecordStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->ProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error('更新消费记录失败');
                exit;
            }

            if($OrderStatus === FALSE || $ProductStatus === FALSE || $BusinessStatus === FALSE || $RecordStatus === FALSE){
                $this->RecordModel->rollback();
                $this->BusinessModel->rollback();
                $this->ProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error('取消订单失败');
                exit;
            }else{
                $this->RecordModel->commit();
                $this->BusinessModel->commit();
                $this->ProductModel->commit();
                $this->OrderModel->commit();
                $this->success('取消订单成功');
                exit;
            }
        }
    }


    
    /**
     * 查看物流信息
     *
     * @ApiTitle    (查看物流信息)
     * @ApiSummary  (查看物流信息)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function express(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');

            // 先判断用户是否存在
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 判断订单是否存在
            $order = $this->OrderModel->find($orderid);
            if(!$order){
                $this->error('订单未知');
                exit;
            }

            if($order['status'] <= '1' || empty($order['expresscode'])){
                $this->error('订单未发货');
                exit;
            }

            // 物流查询
            $express = query_express($order['expresscode']);

            $express['nickname'] = $business['nickname'];
            $express['mobile'] = $business['mobile'];
            // var_dump($express);
            // exit;
            if($express){
                $this->success('查询物流信息成功',null,$express);
                exit;
            }else{
                $this->error('物流信息查询失败');
                exit;
            }
        }
    }

    /**
     * 查看订单信息
     *
     * @ApiTitle    (查看订单信息)
     * @ApiSummary  (查看订单信息)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function info(){
        if($this->request->isPost()){
            // $params = $this->request->param();
            // var_dump($params);
            // exit;
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');

            // 查询客户是否存在
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询订单是否存在
            $list = $this->OrderModel
                ->with(['express','business','address'])
                ->select($orderid);

            if($list){
                foreach($list as $item){
                    $info = $this->OrderProductModel
                        ->with(['products'])
                        ->where(['orderid'=>$orderid])
                        ->select();
                    
                    $info = collection($info)->toArray();
                }   
                $list['products'] = $info;
                // var_dump(collection($list)->toArray());
                // exit;
            }
             
            // var_dump(collection($list)->toArray());
            // exit;
            
            if($list){
                $this->success('返回订单数据成功',null,$list);
                exit;
            }else{
                $this->error('暂无订单数据');
                exit;
            }

        }
    }

    /**
     * 确认收货
     *
     * @ApiTitle    (确认收货)
     * @ApiSummary  (确认收货)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function receive(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$orderid){
                $this->error('订单信息未知');
                exit;
            }

            $data = [
                'id' => $orderid,
                'status' => 3
            ];

            // 修改订单表
            $result = $this->OrderModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('确认收货失败');
                exit;
            }else{
                $this->success('确认收货成功');
                exit;
            }

        }
    }

    /**
     * 获取订单商品名称
     *
     * @ApiTitle    (获取订单商品名称)
     * @ApiSummary  (获取订单商品名称)
     * @ApiMethod   (POST)
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function product(){
        if($this->request->isPost()){
            $orderid = $this->request->param('orderid',0,'trim');

            $product = $this->OrderProductModel->with(['products'])->where(['orderid' => $orderid])->select();

            // var_dump(collection($product)->toArray());
            // var_dump($productid);
            // exit;
            
            foreach($product as $item){
                $where = [
                    'proid' => $item['proid'],
                    'orderid' => $item['orderid']
                ];
                $comment = $this->CommentModel
                        ->where($where)
                        ->find();
                $item['comment'] = $comment['comment'];
            }


            if($product){
                $this->success('返回商品名称成功',null,$product);
                exit;
            }else{
                $this->error('返回商品名称失败');
                exit;
            }
        }
    }

    /**
     * 评价
     *
     * @ApiTitle    (评价)
     * @ApiSummary  (评价)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiParams   (name="proid", type="int", required=true, description="商品ID")
     * @ApiParams   (name="comment", type="string", required=true, description="评价内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function comment(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');
            $proid = $this->request->param('proid',0,'trim');
            $comment = $this->request->param('comment','','trim');

            // 查询客户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$orderid){
                $this->error('订单信息未知');
                exit;
            }

            // 查询订单商品
            $where = [
                'orderid' => $order['id'],
                'proid' => $proid
            ];
            $orderproduct = $this->OrderProductModel->where($where)->select();
            if(!$orderproduct){
                $this->error('订单商品未知');
                exit;
            }

            // 查询商品
            $product = $this->ProductModel->find($proid);
            if(!$product){
                $this->error('商品未知');
                exit;
            }

            $data = [
                'comment' => $comment,
                'busid' => $busid,
                'proid' => $proid,
                'orderid' => $orderid
            ];

            $result = $this->CommentModel->save($data);
            if($result === FALSE){
                $this->error('提交评价失败');
                exit;
            }else{
                $this->success('提交评价成功');
                exit;
            }
            
        }
    }

    /**
     * 删除评价
     *
     * @ApiTitle    (删除评价)
     * @ApiSummary  (删除评价)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiParams   (name="proid", type="int", required=true, description="商品ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function delcomment(){
        // var_dump($this->request->param());
        // exit;
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $proid = $this->request->param('proid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');
    
            // 查询客户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }
    
            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$orderid){
                $this->error('订单信息未知');
                exit;
            }
    
            // 查询订单商品
            $where = [
                'orderid' => $order['id'],
                'proid' => $proid
            ];
            $orderproduct = $this->OrderProductModel->where($where)->select();
            if(!$orderproduct){
                $this->error('订单商品未知');
                exit;
            }
    
            // 查询商品
            $product = $this->ProductModel->find($proid);
            if(!$product){
                $this->error('商品未知');
                exit;
            }
    
            $where = [
                'busid' => $busid,
                'orderid' => $orderid,
                'proid' => $proid
            ];
    
            $commentid = $this->CommentModel->where($where)->value('id');
            // var_dump($comment);
            // exit;
    
            $result = $this->CommentModel->where('id',$commentid)->delete();
    
            if($result === FALSE){
                $this->error('删除评论失败');
                exit;
            }else{
                $this->success('删除评论成功');
                exit;
            }
        }
        
    }

    /**
     * 获取订单商品信息
     *
     * @ApiTitle    (获取订单商品信息)
     * @ApiSummary  (获取订单商品信息)
     * @ApiMethod   (POST)
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function getinfo(){
        // var_dump($this->request->param());
        // exit;
        if($this->request->isPost()){
            $orderid = $this->request->param('orderid',0,'trim');

            // 查询订单
            $order = $this->OrderModel->find($orderid);
            
            if($order){
                $this->success('订单返回成功',null,$order);
                exit;
            }else{
                $this->error('订单信息未知');
                exit;
            }
        }
        


    }

    /**
     * 退货
     *
     * @ApiTitle    (退货)
     * @ApiSummary  (退货)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiParams   (name="proid", type="int", required=true, description="商品ID")
     * @ApiParams   (name="content", type="string", required=true, description="退货原因")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function refund(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');
            $refundreason = $this->request->param('refundreason','','trim');

            // 查询客户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$order){
                $this->error('订单信息未知');
                exit;
            }

            $data = [
                'id' => $orderid,
                'status' => -2,
                'refundreason' => $refundreason
            ];

            $result = $this->OrderModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('退货申请失败');
                exit;
            }else{
                $this->success('退货申请成功');
                exit;
            }
        }
        
    }

    /**
     * 退货信息
     *
     * @ApiTitle    (退货信息)
     * @ApiSummary  (退货信息)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="orderid", type="int", required=true, description="订单ID")
     * @ApiParams   (name="", type="string", required=true, description="退货原因")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function refundinfo(){
        // var_dump($this->request->param());
        // exit;
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');
            $addrid = $this->request->param('addrid',0,'trim');
            $remark = $this->request->param('remark',0,'trim');
            $expressid = $this->request->param('expressid',0,'trim');
            $expresscode = $this->request->param('expresscode',0,'trim');
    
            // 查询客户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }
    
            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$order){
                $this->error('订单信息未知');
                exit;
            }
            // var_dump($order->toArray());
            // exit;
    
            $orderproduct = $this->OrderProductModel->with(['products'])->where(['orderid'=>$orderid])->select();
            if(!$orderproduct){
                $this->error('订单商品信息未知');
                exit;
            }
            // var_dump(collection($orderproduct)->toArray());
            // exit;
    
            // 查询地址
            $address = $this->AddressModel->find($addrid);
            if(!$address){
                $this->error('地址信息未知');
                exit;
            }
            // var_dump($address->toArray());
            // exit;
    
            // 查询物流公司
            $express = $this->ExpressModel->find($expressid);
            if(!$express){
                $this->error('物流公司信息未知');
            }
    
            // 开启事务
            $this->DepotModel->startTrans();
            $this->DepotProductModel->startTrans();
    
            $DepotData = [
                'code' => build_code('BP'),
                'ordercode' => $order['code'],
                'busid' => $busid,
                'contact' => $address['consignee'],
                'phone' => $address['mobile'],
                'address' => $address['address'],
                'province' =>$address['province'],
                'city' => $address['city'],
                'district' => $address['district'],
                'expressid' => $expressid,
                'expresscode' => $expresscode,
                'remark' => $remark,
                'status' => 0,
                'amount' => $order['amount']
            ];
    
            $DepotStatus = $this->DepotModel->validate("common/Depot/Back.back")->save($DepotData);
    
            if($DepotStatus === FALSE){
                $this->error($this->DepotModel->getError());
                exit;
            }
    
            // 存放商品信息
            $DepotProductData = [];
            foreach($orderproduct as $item){
                $DepotProductData[] = [
                    'backid' => $this->DepotModel->id,
                    'proid' => $item['proid'],
                    'nums' => $item['pronum'],
                    'price' => $item['price'],
                    'total' => $item['total']
                ];
            }
    
            $DepotProductStatus = $this->DepotProductModel->validate("commom/Depot/Productback.add")->saveAll($DepotProductData);
    
            if($DepotProductStatus === FALSE){
                $this->DepotModel->rollback();
                $this->error($this->DepotProductModel->getError());
                exit;
            }
    
            if($DepotStatus === FALSE || $DepotProductStatus === FALSE){
                $this->DepotProductModel->rollback();
                $this->DepotModel->rollback();
                $this->error('退货信息提交失败');
                exit;
            }else{
                $this->DepotProductModel->commit();
                $this->DepotModel->commit();
                $this->success('退货信息提交成功','/order/index');
                exit;
            }
        }
        
        

    }


    /**
     * 物流信息
     *
     * @ApiTitle    (物流信息)
     * @ApiSummary  (物流信息)
     * @ApiMethod   (POST)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function expresslist(){
        if($this->request->isPost()){
            $list = $this->ExpressModel->select();
        
            // var_dump(collection($list)->toArray());
            // exit;
            if($list){
                $this->success('返回物流列表',null,$list);
                exit;
            }else{
                $this->error('返回物流列表失败');
                exit;
            }
        }
        
    }

}
